package com.mjf.cjt.domain;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSONObject;
import com.mjf.cjt.util.TMHttpUtils;
import com.mjf.cjt.vo.TMResultListVo;
import lombok.Setter;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Authror pm
 * @Desc
 * @Date 2024-11-27 11:28
 */
@Configuration
@Setter
public class PullDataFromCjtRemote {

    private String token ;

    private List<String>  initSelectFields ;

    private Map<String,Object> paramDic;

    private String path ;

    private TMHttpUtils httpUtils;
    public void listFromRemote(int pageIndex, List<Map<String, String>> resultList) {
        PageQueryMainDto param = new PageQueryMainDto();
        param.setSelectFields(initSelectFields);
        param.setPageSize(100);
        param.setPageIndex(pageIndex);
        param.setParamDic(paramDic);
        JSONObject paramJson = new JSONObject();
        paramJson.put("data", param);
        Map<String, String> header = new HashMap<>();
        header.put("openToken", token);
        HttpResponse response = httpUtils.doPost(path, JSONObject.toJSONString(param), header);
        if (response.isOk()) {
            TMResultListVo result = JSONObject.parseObject(response.body(), TMResultListVo.class);
            if (result != null && "0".equals(result.getCode())) {
                if (result.getData() != null && result.getData().getRows() != null && result.getData().getRows().size() > 0) {
                    result.getData().getRows().forEach(e -> {
                        Map<String, String> map = new HashMap<>();
                        for (int i = 0; i < result.getData().getColumns().size(); i++) {
                            map.put(result.getData().getColumns().get(i), e.get(i));
                        }
                        resultList.add(map);
                    });
                    listFromRemote(++pageIndex, resultList);
                }
            } else {
                throw new RuntimeException("获取单据数据失败:" + result.getMessage());
            }
        } else {
            throw new RuntimeException("获取单据数据失败:" + response.body());

        }

    }
}
